package com.itqf.service.impl;

import com.itqf.entity.User;
import com.itqf.mapper.UserMapper;
import com.itqf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.shiro.crypto.hash.Sha256Hash;

import java.util.Set;
import java.util.UUID;

/**
 * 作者:高少东
 * 项目名:  product_gsd
 * 时间: 2019/12/18 21:22
 * 描述:
 */

@Service
public class UserServiceimpl  implements UserService {

     @Autowired
    UserMapper userMapper;

    /**
     *
     * @param uAccount 查询账号是否存在
     * @return
     */

    @Override
    public String queryAccount(String uAccount) {

        String account = userMapper.queryAccount(uAccount);
        return account;
    }

    /**
     *
     * @param user 注册账号
     * @return
     */
    @Override
    public int registAccount(User user) {

        String salt = UUID.randomUUID().toString();

        String password = new Sha256Hash(user.getUPassword(), salt, 1024).toString();

        User user1 = new User();
         user1.setUSalt(salt);
         user1.setUNickname("老王");
         user1.setUAccount(user.getUAccount());
         user1.setUPassword(password);

        int rows = userMapper.registAccount(user1);

        return rows;
    }

    /**
     *  登录账号  查询账号是否存在  shiro验证
     * @param account
     * @return
     */
    @Override
    public User loginAccount(String account) {

          User user = userMapper.loginAccount(account);
        return user;
    }

    /*
       通过账号查询角色
     */
    @Override
    public Set<String> selectRoleByAccount(String uAccount) {

         Set<String> roles = userMapper.selectRoleByAccount(uAccount);
        return roles;
    }

    /*
       通过角色查询 权限
     */
    @Override
    public Set<String> selectPermissionByRole(Set<String> roles) {

      Set<String> permission = userMapper.selectPermissionByRole(roles);
        return permission;
    }

    /**
     *    修改密码
     * @param uId
     * @param newPassword
     * @return
     */
    @Override
    public int alterPassword(Integer uId,String newPassword) {

          //通过账号id 查询盐
          String salt=userMapper.querySalt(uId);

        String password = new Sha256Hash(newPassword, salt, 1024).toString();

       int rows= userMapper.alterPassword(uId,password);

        return rows;
    }

    /**
     *  验证账号是否是超管 还是普通管理员
     * @param uAccount
     * @return
     */
    @Override
    public String verifyUser(String uAccount) {

        String account = userMapper.queryAccount(uAccount);
        return account;
    }
}
